package com.trillion.toyou;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyOtherCategoryFragment  extends Fragment implements OnClickListener {
	View view = null;
	
	// 플래그먼트 관련
	Fragment frag = null;
	FragmentTransaction trans = null;
	
	
	TextView nname = null;
	TextView grade = null;
	TextView department = null;
	TextView company = null;
	TextView phoneNum = null;
	TextView telNum = null;
	TextView email = null;
	TextView address = null;
	
	Button category = null;
	
	LinearLayout topTitle = null;
	Data modifyData = null;
	
	Bundle bundle = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.result_layout, container, false);
		
		
		MainActivity.page_check = new MainActivity().BACK_OK;
		
		
		bundle = getArguments();
		String nid = getArguments().get("id").toString();
		
		DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);
		
		modifyData = db.selectone(nid);
		

		
		this.trans = getFragmentManager().beginTransaction();

		
		
		this.nname = (TextView)this.view.findViewById(R.id.resultLayout_tvNname);
		this.grade = (TextView)this.view.findViewById(R.id.resultLayout_tvGrade);
		this.department = (TextView)this.view.findViewById(R.id.resultLayout_tvDepartment);
		this.company = (TextView)this.view.findViewById(R.id.resultLayout_tvCompany);
		this.phoneNum = (TextView)this.view.findViewById(R.id.resultLayout_tvPhoneNum);
		this.telNum = (TextView)this.view.findViewById(R.id.resultLayout_tvTelNum);
		this.email = (TextView)this.view.findViewById(R.id.resultLayout_tvEmail);
		this.address = (TextView)this.view.findViewById(R.id.resultLayout_tvAddress);

		this.category = (Button)this.view.findViewById(R.id.resultLayout_handle);
		this.category.setTextColor(Color.WHITE);
		
		if(this.modifyData.getCategory().equals("vip")){
			this.category.setText("그룹 : VIP");
		}else if(this.modifyData.getCategory().equals("company")){
			this.category.setText("그룹 : 회사");
		}else if(this.modifyData.getCategory().equals("friends")){
			this.category.setText("그룹 : 지인");
		}else if(this.modifyData.getCategory().equals("other")){
			this.category.setText("그룹 : 기타");
		}
		
		
		
		ImageButton btnInput = (ImageButton)this.view.findViewById(R.id.resultLayout_btnInput);
		ImageButton btnCancle = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCancle);
		ImageButton btnBakc = (ImageButton)this.view.findViewById(R.id.resultLayout_btnBack);
		
		
		
		ImageButton btnCategory1 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory1);
		ImageButton btnCategory2 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory2);
		ImageButton btnCategory3 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory3);
		ImageButton btnCategory4 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory4);

		
		
		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.result_layout_top);
		
		
		
		
		
		
		
		this.nname.setText(this.modifyData.getNname());
		this.grade.setText(this.modifyData.getGrade());
		this.department.setText(this.modifyData.getDepartment());
		this.company.setText(this.modifyData.getCompany());
		this.phoneNum.setText(this.modifyData.getPhoneNum());
		this.telNum.setText(this.modifyData.getTelNum());
		this.email.setText(this.modifyData.getEmail());
		this.address.setText(this.modifyData.getAddress());

		
		
		
		btnInput.setOnClickListener(this);
		btnCancle.setOnClickListener(this);
		btnBakc.setOnClickListener(this);
		
		btnCategory1.setOnClickListener(this);
		btnCategory2.setOnClickListener(this);
		btnCategory3.setOnClickListener(this);
		btnCategory4.setOnClickListener(this);
		
		
		return view;
	}

	
	@Override
	public void onResume() {
		new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_get, R.drawable.top_bar_off_nfc_get);
		super.onResume();
	}
	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		
		switch (id) {
		case R.id.resultLayout_btnInput:
			
			DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);		
			
			boolean b = db.modifyCategory(this.modifyData.getCategory(), this.modifyData.getNid());
			
			if(b){
				this.frag = new ViewFragment();
				this.trans.replace(R.id.root_layout, this.frag);
				
				Bundle bundle = new Bundle();
				bundle.putInt("id", this.modifyData.getNid());
				
				
				this.frag.setArguments(bundle);
				
				this.trans.addToBackStack(null);
				this.trans.commitAllowingStateLoss();
				Toast.makeText(getActivity(), "명함이 정상적으로 수정되었습니다.", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getActivity(), "명함을 수정하는 도중 오류가 발생되었습니다.", Toast.LENGTH_SHORT).show();
			}
					
			
			break;
		
		case R.id.resultLayout_btnBack:
		case R.id.resultLayout_btnCancle:
			this.frag = new ViewFragment();
			this.trans.replace(R.id.root_layout, this.frag);
			
			Bundle bundle = new Bundle();
			bundle.putInt("id", this.modifyData.getNid());
			
			
			this.frag.setArguments(bundle);
			
			this.trans.addToBackStack(null);
			this.trans.commitAllowingStateLoss();
			
			Toast.makeText(getActivity(), "수정이 취소되었습니다.", Toast.LENGTH_SHORT).show();
			
			
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory1:
			this.category.setText("그룹 : VIP");
			this.modifyData.setCategory("vip");
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory2:
			this.category.setText("그룹 : 회사");
			this.modifyData.setCategory("company");
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory3:
			this.category.setText("그룹 : 지인");
			this.modifyData.setCategory("friends");
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory4:
			this.category.setText("그룹 : 기타");
			this.modifyData.setCategory("other");
			
			break;
		}
		
	}
	
}
